<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>sha2_64bit.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_sha2_64bit.js.html">sha2_64bit.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2014 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class for the 64-bit SHA-2 cryptographic hashes.
<a name="line17"></a> *
<a name="line18"></a> * Variable names follow the notation in FIPS PUB 180-3:
<a name="line19"></a> * http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf.
<a name="line20"></a> *
<a name="line21"></a> * This code borrows heavily from the 32-bit SHA2 implementation written by
<a name="line22"></a> * Yue Zhang (zysxqn@)
<a name="line23"></a> *
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.crypt.Sha2_64bit&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line30"></a>goog.require(&#39;goog.crypt.Hash&#39;);
<a name="line31"></a>goog.require(&#39;goog.math.Long&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Constructs a SHA-2 64-bit cryptographic hash.
<a name="line37"></a> * This constructor should not be used directly to create the object. Rather,
<a name="line38"></a> * one should use the constructor of one of its subclasses.
<a name="line39"></a> * @constructor
<a name="line40"></a> * @param {number} numHashBlocks The size of the output in 16-byte blocks
<a name="line41"></a> * @param {!Array.&lt;number&gt;} initHashBlocks The hash-specific initialization
<a name="line42"></a> *     vector, as a sequence of 32-bit numbers.
<a name="line43"></a> * @extends {goog.crypt.Hash}
<a name="line44"></a> * @struct
<a name="line45"></a> */
<a name="line46"></a>goog.crypt.Sha2_64bit = function(numHashBlocks, initHashBlocks) {
<a name="line47"></a>  goog.crypt.Sha2_64bit.base(this, &#39;constructor&#39;);
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * The number of bytes that are digested in each pass of this hasher.
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   */
<a name="line53"></a>  this.blockSize = 1024 / 8;
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * A chunk holding the currently processed message bytes. Once the chunk has
<a name="line57"></a>   * {@code this.blocksize} bytes, we feed it into [@code computeChunk_}
<a name="line58"></a>   * and reset {@code this.chunk_}.
<a name="line59"></a>   * @private {!Array.&lt;number&gt;}
<a name="line60"></a>   */
<a name="line61"></a>  this.chunk_ = [];
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * Current number of bytes in {@code this.chunk_}.
<a name="line65"></a>   * @private {number}
<a name="line66"></a>   */
<a name="line67"></a>  this.inChunk_ = 0;
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Total number of bytes in currently processed message.
<a name="line71"></a>   * @private {number}
<a name="line72"></a>   */
<a name="line73"></a>  this.total_ = 0;
<a name="line74"></a>
<a name="line75"></a>  /**
<a name="line76"></a>   * Contains data needed to pad messages less than {@code blocksize} bytes.
<a name="line77"></a>   * @private {!Array.&lt;number&gt;}
<a name="line78"></a>   */
<a name="line79"></a>  this.pad_ = goog.array.repeat(0, this.blockSize);
<a name="line80"></a>  this.pad_[0] = 128;
<a name="line81"></a>
<a name="line82"></a>  /**
<a name="line83"></a>   * Holds the previous values of accumulated hash a-h in the
<a name="line84"></a>   * {@code computeChunk_} function.
<a name="line85"></a>   * @private {!Array.&lt;!goog.math.Long&gt;}
<a name="line86"></a>   */
<a name="line87"></a>  this.hash_ = [];
<a name="line88"></a>
<a name="line89"></a>  /**
<a name="line90"></a>   * The number of blocks of output produced by this hash function, where each
<a name="line91"></a>   * block is eight bytes long.
<a name="line92"></a>   * @private {number}
<a name="line93"></a>   */
<a name="line94"></a>  this.numHashBlocks_ = numHashBlocks;
<a name="line95"></a>
<a name="line96"></a>  /**
<a name="line97"></a>   * Temporary array used in chunk computation.  Allocate here as a
<a name="line98"></a>   * member rather than as a local within computeChunk_() as a
<a name="line99"></a>   * performance optimization to reduce the number of allocations and
<a name="line100"></a>   * reduce garbage collection.
<a name="line101"></a>   * @type {!Array.&lt;!goog.math.Long&gt;}
<a name="line102"></a>   * @private
<a name="line103"></a>   */
<a name="line104"></a>  this.w_ = [];
<a name="line105"></a>
<a name="line106"></a>  /**
<a name="line107"></a>   * The value to which {@code this.hash_} should be reset when this
<a name="line108"></a>   * Hasher is reset.
<a name="line109"></a>   * @private {!Array.&lt;!goog.math.Long&gt;}
<a name="line110"></a>   * @const
<a name="line111"></a>   */
<a name="line112"></a>  this.initHashBlocks_ = [];
<a name="line113"></a>  for (var i = 0; i &lt; initHashBlocks.length; i += 2) {
<a name="line114"></a>    this.initHashBlocks_.push(new goog.math.Long(
<a name="line115"></a>        // NIST constants are in big-endian order.  Long is little-endian
<a name="line116"></a>        initHashBlocks[i + 1], initHashBlocks[i]));
<a name="line117"></a>  }
<a name="line118"></a>
<a name="line119"></a>  this.reset();
<a name="line120"></a>};
<a name="line121"></a>goog.inherits(goog.crypt.Sha2_64bit, goog.crypt.Hash);
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Resets this hash function.
<a name="line126"></a> */
<a name="line127"></a>goog.crypt.Sha2_64bit.prototype.reset = function() {
<a name="line128"></a>  this.chunk_ = [];
<a name="line129"></a>  this.inChunk_ = 0;
<a name="line130"></a>  this.total_ = 0;
<a name="line131"></a>  this.hash_ = goog.array.clone(this.initHashBlocks_);
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Updates this hash by processing a given 1024-bit message chunk.
<a name="line137"></a> * @param {!Array.&lt;number&gt;} chunk A 1024-bit message chunk to be processed.
<a name="line138"></a> * @private
<a name="line139"></a> */
<a name="line140"></a>goog.crypt.Sha2_64bit.prototype.computeChunk_ = function(chunk) {
<a name="line141"></a>  goog.asserts.assert(chunk.length == this.blockSize);
<a name="line142"></a>  var rounds = 80;
<a name="line143"></a>  var k = goog.crypt.Sha2_64bit.K_;
<a name="line144"></a>
<a name="line145"></a>  // Divide the chunk into 16 64-bit-words.
<a name="line146"></a>  var w = this.w_;
<a name="line147"></a>  var index = 0;
<a name="line148"></a>  var offset = 0;
<a name="line149"></a>  while (offset &lt; chunk.length) {
<a name="line150"></a>    w[index++] = new goog.math.Long(
<a name="line151"></a>        (chunk[offset + 4] &lt;&lt; 24) | (chunk[offset + 5] &lt;&lt; 16) |
<a name="line152"></a>            (chunk[offset + 6] &lt;&lt; 8) | (chunk[offset + 7]),
<a name="line153"></a>        (chunk[offset] &lt;&lt; 24) | (chunk[offset + 1] &lt;&lt; 16) |
<a name="line154"></a>            (chunk[offset + 2] &lt;&lt; 8) | (chunk[offset + 3]));
<a name="line155"></a>    offset = index * 8;
<a name="line156"></a>  }
<a name="line157"></a>
<a name="line158"></a>  // Extend the w[] array to be the number of rounds.
<a name="line159"></a>  for (var i = 16; i &lt; rounds; i++) {
<a name="line160"></a>    var s0 = this.sigma0_(w[i - 15]);
<a name="line161"></a>    var s1 = this.sigma1_(w[i - 2]);
<a name="line162"></a>    w[i] = this.sum_(w[i - 16], w[i - 7], s0, s1);
<a name="line163"></a>  }
<a name="line164"></a>
<a name="line165"></a>  var a = this.hash_[0];
<a name="line166"></a>  var b = this.hash_[1];
<a name="line167"></a>  var c = this.hash_[2];
<a name="line168"></a>  var d = this.hash_[3];
<a name="line169"></a>  var e = this.hash_[4];
<a name="line170"></a>  var f = this.hash_[5];
<a name="line171"></a>  var g = this.hash_[6];
<a name="line172"></a>  var h = this.hash_[7];
<a name="line173"></a>  for (var i = 0; i &lt; rounds; i++) {
<a name="line174"></a>    var S0 = this.Sigma0_(a);
<a name="line175"></a>    var maj = this.majority_(a, b, c);
<a name="line176"></a>    var t2 = S0.add(maj);
<a name="line177"></a>    var S1 = this.Sigma1_(e);
<a name="line178"></a>    var ch = this.choose_(e, f, g);
<a name="line179"></a>    var t1 = this.sum_(h, S1, ch, k[i], w[i]);
<a name="line180"></a>    h = g;
<a name="line181"></a>    g = f;
<a name="line182"></a>    f = e;
<a name="line183"></a>    e = d.add(t1);
<a name="line184"></a>    d = c;
<a name="line185"></a>    c = b;
<a name="line186"></a>    b = a;
<a name="line187"></a>    a = t1.add(t2);
<a name="line188"></a>  }
<a name="line189"></a>
<a name="line190"></a>  this.hash_[0] = this.hash_[0].add(a);
<a name="line191"></a>  this.hash_[1] = this.hash_[1].add(b);
<a name="line192"></a>  this.hash_[2] = this.hash_[2].add(c);
<a name="line193"></a>  this.hash_[3] = this.hash_[3].add(d);
<a name="line194"></a>  this.hash_[4] = this.hash_[4].add(e);
<a name="line195"></a>  this.hash_[5] = this.hash_[5].add(f);
<a name="line196"></a>  this.hash_[6] = this.hash_[6].add(g);
<a name="line197"></a>  this.hash_[7] = this.hash_[7].add(h);
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/** @override */
<a name="line202"></a>goog.crypt.Sha2_64bit.prototype.update = function(message, opt_length) {
<a name="line203"></a>  if (!goog.isDef(opt_length)) {
<a name="line204"></a>    opt_length = message.length;
<a name="line205"></a>  }
<a name="line206"></a>  // Process the message from left to right up to |opt_length| bytes.
<a name="line207"></a>  // When we get a 512-bit chunk, compute the hash of it and reset
<a name="line208"></a>  // this.chunk_. The message might not be multiple of 512 bits so we
<a name="line209"></a>  // might end up with a chunk that is less than 512 bits. We store
<a name="line210"></a>  // such partial chunk in chunk_ and it will be filled up later
<a name="line211"></a>  // in digest().
<a name="line212"></a>  var n = 0;
<a name="line213"></a>  var inChunk = this.inChunk_;
<a name="line214"></a>
<a name="line215"></a>  // The input message could be either byte array of string.
<a name="line216"></a>  if (goog.isString(message)) {
<a name="line217"></a>    while (n &lt; opt_length) {
<a name="line218"></a>      this.chunk_[inChunk++] = message.charCodeAt(n++);
<a name="line219"></a>      if (inChunk == this.blockSize) {
<a name="line220"></a>        this.computeChunk_(this.chunk_);
<a name="line221"></a>        inChunk = 0;
<a name="line222"></a>      }
<a name="line223"></a>    }
<a name="line224"></a>  } else if (goog.isArray(message)) {
<a name="line225"></a>    while (n &lt; opt_length) {
<a name="line226"></a>      var b = message[n++];
<a name="line227"></a>      if (!(&#39;number&#39; == typeof b &amp;&amp; 0 &lt;= b &amp;&amp; 255 &gt;= b &amp;&amp; b == (b | 0))) {
<a name="line228"></a>        throw Error(&#39;message must be a byte array&#39;);
<a name="line229"></a>      }
<a name="line230"></a>      this.chunk_[inChunk++] = b;
<a name="line231"></a>      if (inChunk == this.blockSize) {
<a name="line232"></a>        this.computeChunk_(this.chunk_);
<a name="line233"></a>        inChunk = 0;
<a name="line234"></a>      }
<a name="line235"></a>    }
<a name="line236"></a>  } else {
<a name="line237"></a>    throw Error(&#39;message must be string or array&#39;);
<a name="line238"></a>  }
<a name="line239"></a>
<a name="line240"></a>  // Record the current bytes in chunk to support partial update.
<a name="line241"></a>  this.inChunk_ = inChunk;
<a name="line242"></a>
<a name="line243"></a>  // Record total message bytes we have processed so far.
<a name="line244"></a>  this.total_ += opt_length;
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/** @override */
<a name="line249"></a>goog.crypt.Sha2_64bit.prototype.digest = function() {
<a name="line250"></a>  var digest = [];
<a name="line251"></a>  var totalBits = this.total_ * 8;
<a name="line252"></a>
<a name="line253"></a>  // Append pad 0x80 0x00*.
<a name="line254"></a>  if (this.inChunk_ &lt; 112) {
<a name="line255"></a>    this.update(this.pad_, 112 - this.inChunk_);
<a name="line256"></a>  } else {
<a name="line257"></a>    this.update(this.pad_, this.blockSize - (this.inChunk_ - 112));
<a name="line258"></a>  }
<a name="line259"></a>
<a name="line260"></a>  // Append # bits in the 64-bit big-endian format.
<a name="line261"></a>  for (var i = 127; i &gt;= 112; i--) {
<a name="line262"></a>    this.chunk_[i] = totalBits &amp; 255;
<a name="line263"></a>    totalBits /= 256; // Don&#39;t use bit-shifting here!
<a name="line264"></a>  }
<a name="line265"></a>  this.computeChunk_(this.chunk_);
<a name="line266"></a>
<a name="line267"></a>  // Finally, output the result digest.
<a name="line268"></a>  var n = 0;
<a name="line269"></a>  for (var i = 0; i &lt; this.numHashBlocks_; i++) {
<a name="line270"></a>    var block = this.hash_[i];
<a name="line271"></a>    var high = block.getHighBits();
<a name="line272"></a>    var low = block.getLowBits();
<a name="line273"></a>    for (var j = 24; j &gt;= 0; j -= 8) {
<a name="line274"></a>      digest[n++] = ((high &gt;&gt; j) &amp; 255);
<a name="line275"></a>    }
<a name="line276"></a>    for (var j = 24; j &gt;= 0; j -= 8) {
<a name="line277"></a>      digest[n++] = ((low &gt;&gt; j) &amp; 255);
<a name="line278"></a>    }
<a name="line279"></a>  }
<a name="line280"></a>  return digest;
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>// I do not know why the authors of SHA2 chose to name their functions
<a name="line286"></a>// ? and ?, i.e. the Greek letter sigma in uppercase and lowercase.
<a name="line287"></a>// The methods here are named sigma0, sigma1, Sigma0, and Sigma1 to be
<a name="line288"></a>// consistent with the NIST specification.  Uggh.
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>/**
<a name="line292"></a> * Calculates the SHA2 64-bit sigma0 function.
<a name="line293"></a> * rotateRight(value, 1) ^ rotateRight(value, 8) ^ (value &gt;&gt;&gt; 7)
<a name="line294"></a> * @private
<a name="line295"></a> *
<a name="line296"></a> * @param {!goog.math.Long} value
<a name="line297"></a> * @return {!goog.math.Long}
<a name="line298"></a> */
<a name="line299"></a>goog.crypt.Sha2_64bit.prototype.sigma0_ = function(value) {
<a name="line300"></a>  var valueLow = value.getLowBits();
<a name="line301"></a>  var valueHigh = value.getHighBits();
<a name="line302"></a>  var low = (valueLow &gt;&gt;&gt; 1) ^ (valueHigh &lt;&lt; 31) ^
<a name="line303"></a>            (valueLow &gt;&gt;&gt; 8) ^ (valueHigh &lt;&lt; 24) ^
<a name="line304"></a>            (valueLow &gt;&gt;&gt; 7) ^ (valueHigh &lt;&lt; 25);
<a name="line305"></a>  var high = (valueHigh &gt;&gt;&gt; 1) ^ (valueLow &lt;&lt; 31) ^
<a name="line306"></a>             (valueHigh &gt;&gt;&gt; 8) ^ (valueLow &lt;&lt; 24) ^
<a name="line307"></a>             (valueHigh &gt;&gt;&gt; 7);
<a name="line308"></a>  return new goog.math.Long(low, high);
<a name="line309"></a>};
<a name="line310"></a>
<a name="line311"></a>
<a name="line312"></a>/**
<a name="line313"></a> * Calculates the SHA2 64-bit sigma1 function.
<a name="line314"></a> * rotateRight(value, 19) ^ rotateRight(value, 61) ^ (value &gt;&gt;&gt; 6)
<a name="line315"></a> * @private
<a name="line316"></a> *
<a name="line317"></a> * @param {!goog.math.Long} value
<a name="line318"></a> * @return {!goog.math.Long}
<a name="line319"></a> */
<a name="line320"></a>goog.crypt.Sha2_64bit.prototype.sigma1_ = function(value) {
<a name="line321"></a>  var valueLow = value.getLowBits();
<a name="line322"></a>  var valueHigh = value.getHighBits();
<a name="line323"></a>  var low = (valueLow &gt;&gt;&gt; 19) ^ (valueHigh &lt;&lt; 13) ^
<a name="line324"></a>            (valueHigh &gt;&gt;&gt; 29) ^ (valueLow &lt;&lt; 3) ^
<a name="line325"></a>            (valueLow &gt;&gt;&gt; 6) ^ (valueHigh &lt;&lt; 26);
<a name="line326"></a>  var high = (valueHigh &gt;&gt;&gt; 19) ^ (valueLow &lt;&lt; 13) ^
<a name="line327"></a>             (valueLow &gt;&gt;&gt; 29) ^ (valueHigh &lt;&lt; 3) ^
<a name="line328"></a>             (valueHigh &gt;&gt;&gt; 6);
<a name="line329"></a>  return new goog.math.Long(low, high);
<a name="line330"></a>};
<a name="line331"></a>
<a name="line332"></a>
<a name="line333"></a>/**
<a name="line334"></a> * Calculates the SHA2 64-bit Sigma0 function.
<a name="line335"></a> * rotateRight(value, 28) ^ rotateRight(value, 34) ^ rotateRight(value, 39)
<a name="line336"></a> * @private
<a name="line337"></a> *
<a name="line338"></a> * @param {!goog.math.Long} value
<a name="line339"></a> * @return {!goog.math.Long}
<a name="line340"></a> */
<a name="line341"></a>goog.crypt.Sha2_64bit.prototype.Sigma0_ = function(value) {
<a name="line342"></a>  var valueLow = value.getLowBits();
<a name="line343"></a>  var valueHigh = value.getHighBits();
<a name="line344"></a>  var low = (valueLow &gt;&gt;&gt; 28) ^ (valueHigh &lt;&lt; 4) ^
<a name="line345"></a>            (valueHigh &gt;&gt;&gt; 2) ^ (valueLow &lt;&lt; 30) ^
<a name="line346"></a>            (valueHigh &gt;&gt;&gt; 7) ^ (valueLow &lt;&lt; 25);
<a name="line347"></a>  var high = (valueHigh &gt;&gt;&gt; 28) ^ (valueLow &lt;&lt; 4) ^
<a name="line348"></a>             (valueLow &gt;&gt;&gt; 2) ^ (valueHigh &lt;&lt; 30) ^
<a name="line349"></a>             (valueLow &gt;&gt;&gt; 7) ^ (valueHigh &lt;&lt; 25);
<a name="line350"></a>  return new goog.math.Long(low, high);
<a name="line351"></a>};
<a name="line352"></a>
<a name="line353"></a>
<a name="line354"></a>/**
<a name="line355"></a> * Calculates the SHA2 64-bit Sigma1 function.
<a name="line356"></a> * rotateRight(value, 14) ^ rotateRight(value, 18) ^ rotateRight(value, 41)
<a name="line357"></a> * @private
<a name="line358"></a> *
<a name="line359"></a> * @param {!goog.math.Long} value
<a name="line360"></a> * @return {!goog.math.Long}
<a name="line361"></a> */
<a name="line362"></a>goog.crypt.Sha2_64bit.prototype.Sigma1_ = function(value) {
<a name="line363"></a>  var valueLow = value.getLowBits();
<a name="line364"></a>  var valueHigh = value.getHighBits();
<a name="line365"></a>  var low = (valueLow &gt;&gt;&gt; 14) ^ (valueHigh &lt;&lt; 18) ^
<a name="line366"></a>            (valueLow &gt;&gt;&gt; 18) ^ (valueHigh &lt;&lt; 14) ^
<a name="line367"></a>            (valueHigh &gt;&gt;&gt; 9) ^ (valueLow &lt;&lt; 23);
<a name="line368"></a>  var high = (valueHigh &gt;&gt;&gt; 14) ^ (valueLow &lt;&lt; 18) ^
<a name="line369"></a>             (valueHigh &gt;&gt;&gt; 18) ^ (valueLow &lt;&lt; 14) ^
<a name="line370"></a>             (valueLow &gt;&gt;&gt; 9) ^ (valueHigh &lt;&lt; 23);
<a name="line371"></a>  return new goog.math.Long(low, high);
<a name="line372"></a>};
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>/**
<a name="line376"></a> * Calculates the SHA-2 64-bit choose function.
<a name="line377"></a> * This function uses the first argument (&quot;value&quot;) as a mask to choose bits
<a name="line378"></a> * from either the second (&quot;one&quot;) arugment if the bit is set or the third
<a name="line379"></a> * argument (&quot;two&quot;) if the bit is not set.
<a name="line380"></a> * @private
<a name="line381"></a> *
<a name="line382"></a> * @param {!goog.math.Long} value
<a name="line383"></a> * @param {!goog.math.Long} one
<a name="line384"></a> * @param {!goog.math.Long} two
<a name="line385"></a> * @return {!goog.math.Long}
<a name="line386"></a> */
<a name="line387"></a>goog.crypt.Sha2_64bit.prototype.choose_ = function(value, one, two) {
<a name="line388"></a>  var valueLow = value.getLowBits();
<a name="line389"></a>  var valueHigh = value.getHighBits();
<a name="line390"></a>  return new goog.math.Long(
<a name="line391"></a>      (valueLow &amp; one.getLowBits()) | (~valueLow &amp; two.getLowBits()),
<a name="line392"></a>      (valueHigh &amp; one.getHighBits()) | (~valueHigh &amp; two.getHighBits()));
<a name="line393"></a>};
<a name="line394"></a>
<a name="line395"></a>
<a name="line396"></a>/**
<a name="line397"></a> * Calculates the SHA-2 64-bit majority function.
<a name="line398"></a> * This function returns, for each bit position, the bit held by the majority
<a name="line399"></a> * of its three arguments.
<a name="line400"></a> * @private
<a name="line401"></a> *
<a name="line402"></a> * @param {!goog.math.Long} one a voter
<a name="line403"></a> * @param {!goog.math.Long} two another voter
<a name="line404"></a> * @param {!goog.math.Long} three another voter
<a name="line405"></a> * @return {!goog.math.Long}
<a name="line406"></a> */
<a name="line407"></a>goog.crypt.Sha2_64bit.prototype.majority_ = function(one, two, three) {
<a name="line408"></a>  return new goog.math.Long(
<a name="line409"></a>      (one.getLowBits() &amp; two.getLowBits()) |
<a name="line410"></a>          (two.getLowBits() &amp; three.getLowBits()) |
<a name="line411"></a>          (one.getLowBits() &amp; three.getLowBits()),
<a name="line412"></a>      (one.getHighBits() &amp; two.getHighBits()) |
<a name="line413"></a>          (two.getHighBits() &amp; three.getHighBits()) |
<a name="line414"></a>          (one.getHighBits() &amp; three.getHighBits()));
<a name="line415"></a>};
<a name="line416"></a>
<a name="line417"></a>
<a name="line418"></a>/**
<a name="line419"></a> * Adds two or more goog.math.Long values.
<a name="line420"></a> * @private
<a name="line421"></a> *
<a name="line422"></a> * @param {!goog.math.Long} one first summand
<a name="line423"></a> * @param {!goog.math.Long} two second summand
<a name="line424"></a> * @param {...goog.math.Long} var_args more arguments to sum
<a name="line425"></a> * @return {!goog.math.Long} The resulting sum.
<a name="line426"></a> */
<a name="line427"></a>goog.crypt.Sha2_64bit.prototype.sum_ = function(one, two, var_args) {
<a name="line428"></a>  // The low bits may be signed, but they represent a 32-bit unsigned quantity.
<a name="line429"></a>  // We must be careful to normalize them.
<a name="line430"></a>  // This doesn&#39;t matter for the high bits.
<a name="line431"></a>  var low = (one.getLowBits() ^ 0x80000000) + (two.getLowBits() ^ 0x80000000);
<a name="line432"></a>  var high = one.getHighBits() + two.getHighBits();
<a name="line433"></a>  for (var i = arguments.length - 1; i &gt;= 2; --i) {
<a name="line434"></a>    low += (arguments[i].getLowBits() ^ 0x80000000);
<a name="line435"></a>    high += arguments[i].getHighBits();
<a name="line436"></a>  }
<a name="line437"></a>  // Because of the ^0x80000000, each value we added is 0x80000000 too small
<a name="line438"></a>  // Add arguments.length * 0x80000000 to the current sum
<a name="line439"></a>  if (arguments.length &amp; 1) {
<a name="line440"></a>    low += 0x80000000;
<a name="line441"></a>  }
<a name="line442"></a>  high += (arguments.length &gt;&gt; 1);
<a name="line443"></a>  high += Math.floor(low / 0x100000000);
<a name="line444"></a>  return new goog.math.Long(low, high);
<a name="line445"></a>};
<a name="line446"></a>
<a name="line447"></a>
<a name="line448"></a>/**
<a name="line449"></a> * Constants used in SHA-512 variants
<a name="line450"></a> * @const
<a name="line451"></a> * @private {!Array.&lt;!goog.math.Long&gt;}
<a name="line452"></a> */
<a name="line453"></a>goog.crypt.Sha2_64bit.K_ = (function() {
<a name="line454"></a>  var int64 = function(a, b) {
<a name="line455"></a>    // NIST constants are big-endian.  Need to reverse endianness.
<a name="line456"></a>    return new goog.math.Long(b, a);
<a name="line457"></a>  };
<a name="line458"></a>  return [
<a name="line459"></a>    int64(0x428a2f98, 0xd728ae22), int64(0x71374491, 0x23ef65cd),
<a name="line460"></a>    int64(0xb5c0fbcf, 0xec4d3b2f), int64(0xe9b5dba5, 0x8189dbbc),
<a name="line461"></a>    int64(0x3956c25b, 0xf348b538), int64(0x59f111f1, 0xb605d019),
<a name="line462"></a>    int64(0x923f82a4, 0xaf194f9b), int64(0xab1c5ed5, 0xda6d8118),
<a name="line463"></a>    int64(0xd807aa98, 0xa3030242), int64(0x12835b01, 0x45706fbe),
<a name="line464"></a>    int64(0x243185be, 0x4ee4b28c), int64(0x550c7dc3, 0xd5ffb4e2),
<a name="line465"></a>    int64(0x72be5d74, 0xf27b896f), int64(0x80deb1fe, 0x3b1696b1),
<a name="line466"></a>    int64(0x9bdc06a7, 0x25c71235), int64(0xc19bf174, 0xcf692694),
<a name="line467"></a>    int64(0xe49b69c1, 0x9ef14ad2), int64(0xefbe4786, 0x384f25e3),
<a name="line468"></a>    int64(0x0fc19dc6, 0x8b8cd5b5), int64(0x240ca1cc, 0x77ac9c65),
<a name="line469"></a>    int64(0x2de92c6f, 0x592b0275), int64(0x4a7484aa, 0x6ea6e483),
<a name="line470"></a>    int64(0x5cb0a9dc, 0xbd41fbd4), int64(0x76f988da, 0x831153b5),
<a name="line471"></a>    int64(0x983e5152, 0xee66dfab), int64(0xa831c66d, 0x2db43210),
<a name="line472"></a>    int64(0xb00327c8, 0x98fb213f), int64(0xbf597fc7, 0xbeef0ee4),
<a name="line473"></a>    int64(0xc6e00bf3, 0x3da88fc2), int64(0xd5a79147, 0x930aa725),
<a name="line474"></a>    int64(0x06ca6351, 0xe003826f), int64(0x14292967, 0x0a0e6e70),
<a name="line475"></a>    int64(0x27b70a85, 0x46d22ffc), int64(0x2e1b2138, 0x5c26c926),
<a name="line476"></a>    int64(0x4d2c6dfc, 0x5ac42aed), int64(0x53380d13, 0x9d95b3df),
<a name="line477"></a>    int64(0x650a7354, 0x8baf63de), int64(0x766a0abb, 0x3c77b2a8),
<a name="line478"></a>    int64(0x81c2c92e, 0x47edaee6), int64(0x92722c85, 0x1482353b),
<a name="line479"></a>    int64(0xa2bfe8a1, 0x4cf10364), int64(0xa81a664b, 0xbc423001),
<a name="line480"></a>    int64(0xc24b8b70, 0xd0f89791), int64(0xc76c51a3, 0x0654be30),
<a name="line481"></a>    int64(0xd192e819, 0xd6ef5218), int64(0xd6990624, 0x5565a910),
<a name="line482"></a>    int64(0xf40e3585, 0x5771202a), int64(0x106aa070, 0x32bbd1b8),
<a name="line483"></a>    int64(0x19a4c116, 0xb8d2d0c8), int64(0x1e376c08, 0x5141ab53),
<a name="line484"></a>    int64(0x2748774c, 0xdf8eeb99), int64(0x34b0bcb5, 0xe19b48a8),
<a name="line485"></a>    int64(0x391c0cb3, 0xc5c95a63), int64(0x4ed8aa4a, 0xe3418acb),
<a name="line486"></a>    int64(0x5b9cca4f, 0x7763e373), int64(0x682e6ff3, 0xd6b2b8a3),
<a name="line487"></a>    int64(0x748f82ee, 0x5defb2fc), int64(0x78a5636f, 0x43172f60),
<a name="line488"></a>    int64(0x84c87814, 0xa1f0ab72), int64(0x8cc70208, 0x1a6439ec),
<a name="line489"></a>    int64(0x90befffa, 0x23631e28), int64(0xa4506ceb, 0xde82bde9),
<a name="line490"></a>    int64(0xbef9a3f7, 0xb2c67915), int64(0xc67178f2, 0xe372532b),
<a name="line491"></a>    int64(0xca273ece, 0xea26619c), int64(0xd186b8c7, 0x21c0c207),
<a name="line492"></a>    int64(0xeada7dd6, 0xcde0eb1e), int64(0xf57d4f7f, 0xee6ed178),
<a name="line493"></a>    int64(0x06f067aa, 0x72176fba), int64(0x0a637dc5, 0xa2c898a6),
<a name="line494"></a>    int64(0x113f9804, 0xbef90dae), int64(0x1b710b35, 0x131c471b),
<a name="line495"></a>    int64(0x28db77f5, 0x23047d84), int64(0x32caab7b, 0x40c72493),
<a name="line496"></a>    int64(0x3c9ebe0a, 0x15c9bebc), int64(0x431d67c4, 0x9c100d4c),
<a name="line497"></a>    int64(0x4cc5d4be, 0xcb3e42b6), int64(0x597f299c, 0xfc657e2a),
<a name="line498"></a>    int64(0x5fcb6fab, 0x3ad6faec), int64(0x6c44198c, 0x4a475817)
<a name="line499"></a>  ];
<a name="line500"></a>})();
</pre>


</body>
</html>
